package com.shop2cn.iapi.sqdistributor.testcase;

import java.math.BigDecimal;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.sqdistributor.parameter.request.SavePartnerConfigReq;
import com.shop2cn.iapi.sqdistributor.service.SavePartnerConfigCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.database.model.sqdistributor.PartnerConfig;
import com.ymttest.database.model.sqdistributor.PartnerRewardRate;
import com.ymttest.database.sqlwapper.sqdistributor.PartnerConfigWrapper;
import com.ymttest.database.sqlwapper.sqdistributor.PartnerRewardRateWrapper;
import com.ymttest.utils.DataUtil;

public class Ts_SavePartnerConfig {

	// java接口
	private static SavePartnerConfigReq savePartnerConfigReq;
	private static SavePartnerConfigCall savePartnerConfigCall;

	PartnerConfigWrapper partnerConfigWrapper = new PartnerConfigWrapper();
	PartnerRewardRateWrapper partnerRewardRateWrapper = new PartnerRewardRateWrapper();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("合伙人设置");
	}

	@Before
	public void caseUp() {
		savePartnerConfigReq = new SavePartnerConfigReq();
		savePartnerConfigCall = new SavePartnerConfigCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(savePartnerConfigCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	public void verifyDb() throws Exception {

		PartnerConfig partnerConfig = partnerConfigWrapper
				.selectByMerchantId(savePartnerConfigReq.getMerchantId());

		Logger.comment("验证partner_config表");
		Logger.verifyEquals(true, partnerConfig == null ? false : true,
				"验证partnerConfig是否存在");

		Logger.comment("验证partner_reward_rate表");
		PartnerRewardRate partnerRewardRate = partnerRewardRateWrapper
				.selectByConfigId(partnerConfig.getId());

		Logger.verifyEquals(savePartnerConfigReq.getMerchantId(),
				partnerRewardRate.getMerchant_id(), "验证merchant_id");
		Logger.verifyEquals(savePartnerConfigReq.getPartnerLevelIndex(),
				partnerRewardRate.getPartner_level_index(),
				"验证Partner_level_index");
		Logger.verifyEquals(savePartnerConfigReq.getPartnerLevelLabel(),
				partnerRewardRate.getPartner_level_label(),
				"验证Partner_level_label");
		DataUtil.verifyDecimalEquals(
				savePartnerConfigReq.getLayer1RewardRate(),
				partnerRewardRate.getLayer1_reward_rate(),
				"验证Layer1_reward_rate");
		DataUtil.verifyDecimalEquals(
				savePartnerConfigReq.getLayer2RewardRate(),
				partnerRewardRate.getLayer2_reward_rate(),
				"验证Layer2_reward_rate");
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SavePartnerConfig_001() {
		Logger.start(true, "设置普通分销机构的分销员配置信息，设置成功");

		try {

			savePartnerConfigReq.setMerchantId(6002312456L);
			savePartnerConfigReq.setPartnerLevelIndex(0);
			savePartnerConfigReq.setPartnerLevelLabel("autotest");
			savePartnerConfigReq.setLayer1RewardRate(BigDecimal.ONE);
			savePartnerConfigReq.setLayer2RewardRate(BigDecimal.TEN);

			savePartnerConfigCall.setData(savePartnerConfigReq);
			savePartnerConfigCall.callService();

			Logger.verifyEquals(true, savePartnerConfigCall.getSuccess(),
					"验证success");

			verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
